package com.atguigu.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * ClassName: CompletableFutureDemo1
 * Package: com.atguigu.ssyx
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-04
 * Time: 13:11
 */

//CompletableFuture 任务的组合
public class CompletableFutureDemo4 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //创建线程池
        ExecutorService service = Executors.newFixedThreadPool(3);

        //1 任务1 返回结果 1024
        CompletableFuture<Integer> fuA = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + ":begin..");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int value = 1024;
            System.out.println("任务一 :" + value);
            System.out.println(Thread.currentThread().getName() + ":end..");
            return value;
        }, service);

        //任务2 返回结果 200
        CompletableFuture<Integer> fuB = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + ":begin..");
            int value = 200;
            System.out.println("任务二 :" + value);
            System.out.println(Thread.currentThread().getName() + ":end..");
            return value;
        }, service);

        //两个任务都完成之后往下之行
        CompletableFuture<Void> all = CompletableFuture.allOf(fuA, fuB);
        all.get(); //得到计算结果
        System.out.println("over ...");


    }

}
